package com.zhan.ielts.web.controller;

import java.sql.Timestamp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.CustomResult;
import com.zhan.ielts.domain.Param;
import com.zhan.ielts.domain.query.ParamQuery;
import com.zhan.ielts.service.ParamService;

@Controller
@RequestMapping("/param")
public class ParamController extends BaseControllerImpl<Param, ParamQuery> {

	private Logger log = LoggerFactory.getLogger(ParamController.class);

	@Autowired
	private ParamService paramService;

	@Override
	protected BaseService<Param> getBaseService() {
		return paramService;
	}

	@Override
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView addOne(Param entity) {
		entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		try {
			paramService.insert(entity);
		} catch (Exception e) {
			return new ModelAndView(path.getRedirectListPath());
		}
		ModelAndView mav = new ModelAndView(path.getRedirectListPath() + "?type=" + entity.getType());
		return mav;
	}

	@Override
	@RequestMapping(method = RequestMethod.PUT)
	public ModelAndView editOne(Param entity) {
		entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		paramService.updateByIdSelective(entity);
		Param query = new Param();
		query.setType(entity.getType());
		ModelAndView mav = new ModelAndView(path.getRedirectListPath() + "?type=" + entity.getType());
		mav.addObject("query", query);
		return mav;
	}

	@ResponseBody
	@RequestMapping(value = "/fetch", method = RequestMethod.GET)
	public CustomResult fetch(ParamQuery query, @PageableDefault Pageable pageable) {
		Param param = paramService.queryOne(query);
		return new CustomResult(1,"",param);
	}
	
}
